package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import javax.security.auth.login.LoginException;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    public boolean checkPhone(String phone) {
        int count = userInfoMapper.selectByPhone(phone);
        return count >0 ;
    }

    public void sendVerifyCode(String phone) {
        //创建短信验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        StringBuilder sb = new StringBuilder();
        sb.append("您的验证码是:").append(code).append("请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内使用");
        //发送验证码
        System.out.println(sb.toString());
        //把验证码放到redis中缓存
        userInfoRedisService.setVerifyCode(phone,code);

    }

    public AjaxResult userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) throws LogicException{
        AjaxResult result = new AjaxResult();
            //检验是否为空
            AssertUtil.hasLength(phone, "手机号不能为空");
            AssertUtil.hasLength(nickname, "昵称不能为空");
            AssertUtil.hasLength(password, "密码不能为空");
            AssertUtil.hasLength(rpassword, "确认密码不能为空");
            AssertUtil.hasLength(verifyCode, "验证码不能为空");
            //两次密码是否一致
            AssertUtil.isEquals(password, rpassword, "两次密码输入不一致");

            //手机号格式是否正确
            Pattern pattern = Pattern.compile("^1[3,5]\\d{9}||18[6,8,9]\\d{8}$");
            Matcher matcher = pattern.matcher(phone);

            if ( !matcher.matches()) {
                throw new LogicException("手机号格式不正确");
            }
            //手机号是否唯一
            if (userInfoMapper.selectByPhone(phone) >0){
                throw new LogicException("手机号已被注册");
            }

            //验证码是否正确
            String code = userInfoRedisService.getVerifyCode(phone);
            if (code == null){
                throw new LogicException("您还没有获取验证码");
            }
            AssertUtil.equalsIgnoreCase(code, verifyCode, "验证码输入不正确");
            //完成注册
            UserInfo userInfo = new UserInfo();
            userInfo.setCity("广州");
            userInfo.setInfo("哈哈");
            userInfo.setLevel(1);
            userInfo.setNickname(nickname);
            userInfo.setPassword(password);
            userInfo.setPhone(phone);
            userInfo.setState(UserInfo.STATE_NORMAL);
            userInfo.setHeadImgUrl("/2b4dc077-659b-4fd1-afda-901f3b842463.jpg");
            userInfoMapper.insert(userInfo);
        return result;
    }

    public String userLogin(String username, String password) throws LogicException{
        //检验非空
        AssertUtil.hasLength(username,"手机号不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        //查询用户是否存在
        UserInfo userInfo = userInfoMapper.selectByPhoneAndPassword(username,password);
        if (userInfo == null){
            throw new LogicException("账号或密码输入有误");
        }
        //user存在,创建token变量,将token作为key,user信息作为value存入redis中
        String token = UUID.randomUUID().toString().replaceAll("-", "");

        userInfoRedisService.setUserToken(token,userInfo);

        return token;
    }

    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }

}
